﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext.Web;
public partial class Scores_View : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            StoreCompanyBind();
        }

        CXSecurity.ChkBoss(Cookies.getCookies("cuid"));
    }
    protected void StoreList_onRefresh(object s,StoreRefreshDataEventArgs e)
    {
        SqlQuery q = new Select(new Aggregate("guid", "guid", AggregateFunction.GroupBy)
                                , new Aggregate("companyid", "companyid", AggregateFunction.GroupBy)
                                , new Aggregate("status", "status", AggregateFunction.GroupBy)
                                , new Aggregate("username", "username", AggregateFunction.GroupBy)
                                , new Aggregate("deptname", "deptname", AggregateFunction.GroupBy)
            ).From(VwChengxindetail.Schema).Where("companyid").IsEqualTo(e.Parameters["companyid"]);
        StoreList.DataSource = q.ExecuteDataSet();
        StoreList.DataBind();
    }
    protected void StoreDetail_onRefresh(object sender, StoreRefreshDataEventArgs e)
    {
        string id = e.Parameters["guid"];

        Query q = new Query(VwChengxindetail.Schema);
        q.SetSelectList("id,companyid,guid,scores,classname,tagname,detail,typename,shidu,basescores");
        q.AddWhere("guid", id);
        StoreDetail.DataSource = q.ExecuteDataSet();
        StoreDetail.DataBind();
    }
    protected void StoreCompanyBind()
    {
        Query q = new Query(VwCompanybase.Schema);
        q.SetSelectList("companyid,companyname,companyaddress,electricname,tradename,deptname,scores,credit");
        q.AddWhere("depid", Cookies.getCookies("cdeptid"));
        StoreCompany.DataSource = q.ExecuteDataSet();
        StoreCompany.DataBind();
    }
    protected void Allow(object s, AjaxEventArgs e)
    {
        if(e.ExtraParams["cmd"].ToLower()=="allow")
        {
            int count = new Select(new Aggregate("scores", "scores", AggregateFunction.Sum),
                           new Aggregate("guid", "guid", AggregateFunction.GroupBy)).From(CxChengxin.Schema)
                    .Where("guid").IsEqualTo(e.ExtraParams["guid"]).ExecuteScalar<int>();
            Query q = new Query(CxCompanyBase.Schema);
            q.AddUpdateSetting("scores", count);
            q.AddUpdateSetting("credit", Credit(count));
            q.AddWhere("companyid", e.ExtraParams["companyid"]);
            q.Execute();

            Query q1 = new Query(CxChengxin.Schema);
            q1.AddUpdateSetting("status", 1);
            q1.AddWhere("guid", e.ExtraParams["guid"]);
            q1.Execute();
        }
        if(e.ExtraParams["cmd"].ToLower()=="unallow")
        {
            Query q = new Query(CxChengxin.Schema);
            q.AddUpdateSetting("status", 2);
            q.AddWhere("guid", e.ExtraParams["guid"]);
            q.Execute();
        }
        Ext.MessageBox.Show(new MessageBox.Config
        {
            Buttons = MessageBox.Button.OK,
            Icon = MessageBox.Icon.INFO,
            Title = "操作",
            Message = "成功！"
        });
    }
    protected string Credit(int i)
    {
        if((i>=95)&&(i<=100))
        {
            return "AAA";
        }
        if((i<95)&&(i>=85))
        {
            return "AA";
        }
        if((i<85)&&(i>=75))
        {
            return "A";
        }
        if((i<75)&&(i>=60))
        {
            return "B";
        }
        return "C";
    }
}
